package com.hjm.project.banner.info.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.config.CustomConfig;
import com.hjm.project.agentInfo.agentInfo.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.banner.info.mapper.BannerInfoMapper;
import com.hjm.project.banner.info.domain.BannerInfo;
import com.hjm.project.banner.info.service.IBannerInfoService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 首页banner图Service业务层处理
 * 
 * @author generator
 * @date 2020-11-26
 */
@Service
public class BannerInfoServiceImpl implements IBannerInfoService 
{
    @Autowired
    private BannerInfoMapper bannerInfoMapper;

    /**
     * 查询首页banner图
     * 
     * @param id 首页banner图ID
     * @return 首页banner图
     */
    @Override
    public BannerInfo selectBannerInfoById(Long id)
    {
        return bannerInfoMapper.selectBannerInfoById(id);
    }

    /**
     * 查询首页banner图列表
     * 
     * @param bannerInfo 首页banner图
     * @return 首页banner图
     */
    @Override
    public List<BannerInfo> selectBannerInfoList(BannerInfo bannerInfo){
        List<BannerInfo> bannerInfos = bannerInfoMapper.selectBannerInfoList(bannerInfo);
        for (BannerInfo banner : bannerInfos) {
            banner.setUrl(CustomConfig.getImgUrl() + banner.getUrl());
        }
        return bannerInfos;
    }

    /**
     * 新增首页banner图
     * 
     * @param bannerInfo 首页banner图
     * @return 结果
     */
    @Override
    public int insertBannerInfo(MultipartFile file, BannerInfo bannerInfo) throws Exception {
        //判断格式是不是图片格式jpg png gif
        if (file!=null){
            String fileName =  file.getOriginalFilename();
            if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")||fileName.endsWith(".GIF")||fileName.endsWith(".JPG")||fileName.endsWith(".PNG")) {
                //上传文件
                String urlName = uploadImg(file);
                bannerInfo.setUrl(urlName);
                bannerInfo.setHrelUrl(urlName);
            }else{
                throw  new BusinessException("图片上传失败,请重新上传");
            }
        }else{
            throw  new BusinessException("图片上传失败,未选择图片");
        }


        return bannerInfoMapper.insertBannerInfo(bannerInfo);
    }

    /**
     * 修改首页banner图
     * 
     * @param bannerInfo 首页banner图
     * @return 结果
     */
    @Override
    public int updateBannerInfo(MultipartFile file, BannerInfo bannerInfo) throws Exception {
        //判断格式是不是图片格式jpg png gif
        String fileName =  file.getOriginalFilename();
        if (file != null && StringUtils.isNotBlank(fileName)){
            if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")||fileName.endsWith(".GIF")||fileName.endsWith(".JPG")||fileName.endsWith(".PNG")) {
                //上传文件
                String urlName = uploadImg(file);
                bannerInfo.setUrl(urlName);
                bannerInfo.setHrelUrl(urlName);
            }else{
                throw  new BusinessException("图片上传失败,请重新上传");
            }
        }
        return bannerInfoMapper.updateBannerInfo(bannerInfo);
    }

    /**
     * 删除首页banner图对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBannerInfoByIds(String ids)
    {
        return bannerInfoMapper.deleteBannerInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除首页banner图信息
     * 
     * @param id 首页banner图ID
     * @return 结果
     */
    @Override
    public int deleteBannerInfoById(Long id)
    {
        return bannerInfoMapper.deleteBannerInfoById(id);
    }

    public String uploadImg(MultipartFile imgFile) throws IOException {
        String clientName = imgFile.getOriginalFilename();
        if (StringUtils.isEmpty(clientName)) {
            return null;
        }
        String fileName = null;
        fileName = clientName.substring(clientName.indexOf("."), clientName.length());
        //临时文件
        File dfile = File.createTempFile("prefix", "_" + clientName);
        imgFile.transferTo(dfile);
        //上传至Fast的图片数据
        Map<String, String> metaList = new HashMap<String, String>();
        metaList.put("fileName", fileName);
        metaList.put("fileType", imgFile.getName());
        metaList.put("author", ShiroUtils.getSysUser().getLoginName());
        metaList.put("date", Utils.formateDate(2));//年月日 时分秒
        String fid = FastDFSClient.uploadFile(dfile, dfile.getName(), metaList);
        if (StringUtils.isEmpty(fid)) {
            throw  new BusinessException("图片上传失败,请重新上传");
        }
        return fid;
    }
}
